package com.zysc.processflow.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zysc.processflow.common.base.ResultBody;
import com.zysc.processflow.common.util.StringUtils;
import com.zysc.processflow.entity.*;
import com.zysc.processflow.pojo.HandleRecordPO;
import com.zysc.processflow.pojo.HandlerecorddetailPO;
import com.zysc.processflow.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import com.zysc.processflow.common.base.BaseController;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * <p>
 * 处置流程记录详情表 前端控制器
 * </p>
 *
 * @author 正元智慧城市_
 * @since 2022-11-18
 */
@Api(value = "处置流程记录详情", tags = "处置流程记录详情")
@RestController
@RequestMapping("/handlerecorddetail")
public class HandlerecorddetailController extends BaseController<Handlerecorddetail> {
    private static Logger logger = LoggerFactory.getLogger(HandlerecorddetailController.class);
    @Autowired
    IHandlerecorddetailService service;
    @Autowired
    IHandlerecordService handlerecordService;
    @Autowired
    IHandleprocessnodeService handleprocessnodeService;
    @Autowired
    IHandletaskService handletaskService;
    @Autowired
    IProcessOperationService processOperationService;
    @Autowired
    private IAttachmentService fileService;

    public HandlerecorddetailController(IHandlerecorddetailService service) {
        super(service);
    }

    @ApiOperation("获取处置流程记录详情列表(支持分页)")
    @GetMapping(value = "GetHandlerecorddetailList")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "pageIndex", name = "pageIndex", dataType = "String"),
            @ApiImplicitParam(value = "pageSize", name = "pageSize", dataType = "String"),
            @ApiImplicitParam(value = "处置人姓名", name = "disposername", dataType = "String")
    })
    public ResultBody GetHandlerecorddetailList(Integer pageIndex , Integer pageSize , String disposername) {
        if (pageIndex != null && pageSize != null) {
            IPage<Handlerecorddetail> page = new Page<>(pageIndex, pageSize);
            List<Handlerecorddetail> res = service.GetHandlerecorddetailListByPage(page, disposername);
            return ResultBody.Success(res, page.getTotal());
        } else {
            List<Handlerecorddetail> res = service.GetHandlerecorddetailListByPage(null, disposername);
            return ResultBody.Success(res);
        }
    }

    @ApiOperation("根据recordid获取处置流程记录")
    @GetMapping(value = "GetApprovalHistoryByRecordId")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "记录id", name = "recordid", dataType = "String"),
    })
    public ResultBody GetApprovalHistoryByRecordId(String recordid) {
        List<Handlerecorddetail> res = service.GetApprovalHistoryByRecordId(recordid);
        return ResultBody.Success(res);
    }

    @ApiOperation("审批接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "处置流程记录详情实体", name = "entity", dataType = "Handlerecorddetail")
    })
    @Transactional
    @PostMapping(value = "SaveOrUpdateDetail")
    public ResultBody SaveOrUpdateDetail(@RequestBody Handlerecorddetail entity) {
        try{
            Boolean b1 = true;
            Boolean b2 = true;
            //0.判断是不是处置过了 ,如果处置过了 ,那么不允许再次处置!0714
            //1.保存起来审批信息
            b1 = service.saveOrUpdate(entity);
            //1.1 处理附件
            List<Attachment> attachments = entity.getAttachments();
            if (attachments!=null && attachments.size() > 0) {
                fileService.UpdateLinkAttachment(entity.getId(), "handlerecorddetail", attachments);
            }
            //2.
            String recordid = entity.getRecordid();
            Handlerecord handlerecord = handlerecordService.getById(recordid);
            if(StringUtils.equalsAndNotNull(entity.getType(),"处置任务")){//只有处置任务能修改表
                //1.查询记录record
                Handlerecord handlerecordBefore = handlerecordService.getById(entity.getRecordid());
                //2.pushnode
                processOperationService.pushNode(handlerecordBefore,entity.getNodeid());
                //3.查询之后的记录record
                Handlerecord handlerecordAfter = handlerecordService.getById(entity.getRecordid());
                //4.判断此时record的状态,做不同的操作,让业务系统去做
                if("0".equals(handlerecordAfter.getHandlestatus())){//没处置完成的话(应该不会走这个了,因为现在的报警处置都是一个节点)
                }else if("1".equals(handlerecordAfter.getHandlestatus())){//反馈完成

                }
            }
            if (!b1 || !b2 ) {
                throw new RuntimeException("审批失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultBody.error(e.getMessage(), 500);
        }
        //8.拿到此时的HandleRecordPO当作返回值
        HandleRecordPO result = handlerecordService.getRecordFullInfoByRecordId(entity.getRecordid());
        return ResultBody.Success(result);
    }

    @ApiOperation("删除处置流程记录详情记录")
    @PostMapping("DeleteHandlerecorddetail")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "处置流程记录详情实体id", name = "id", dataType = "String")
    })
    public ResultBody DeleteHandlerecorddetail(String id) {
        if (id != null) {
            return ResultBody.Success(service.removeById(id));
        }
        return ResultBody.error("参数不能为空", 400);
    }
}
